/* File:       wifi.c
* Function:    supply the play moudle function
* Creator:     LDM
* Data:        2008.09.23
* status:      create
* History:
*/

/******************************************************************************
*****************************head file included area***************************
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

#include <gtk/gtk.h>

#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <linux/wireless.h>
#include <gdk/gdkkeysyms.h>

#include "wifi.h"

/******************************************************************************
****************************macro definition area******************************
******************************************************************************/
#define DEBUG 1
#ifdef DEBUG
#define DBG(txt, args... ) fprintf(stderr, "WIRELESS-LAN-DEBUG: " txt , ##args )
#else
#define DBG(txt, args... ) /* nothing */
#endif

#define U_NLS
#ifdef U_NLS
#include <libintl.h>
#include <locale.h>
#define _(text) gettext(text)
#define __(text) gettext(text)
#else
#define _(text) text
#define __(text) text
#endif

#define WIRELESS_LAST_CONF "/etc/wireless-last.conf"
#define NET_CONFIG_FOLDER       "/etc/network/"
#define WEP_CHECK

/******************************************************************************
***************************structure definition area***************************
******************************************************************************/
enum
{
    LINK_UP=1,
    LINK_DOWN,
    NO_LINK
};

enum
{
    COL_ESSID = 0,
    COL_MODE,
    COL_KEY,
    COL_QUALITY,
    COL_STATE,
    n_cols
};

enum
{
	LIST_ALIAS = 0,
	LIST_ESSID,
	LIST_MODE,
	LIST_ENC,
	LIST_KEY,
    LIST_KEY_MGMT,   //add shuai
    LIST_KEY_INDEX,
	LIST_END
};

typedef enum
{
    RESPONSE_DHCP = 1,
    RESPONSE_STATIC
}Response;

/******************************************************************************
***************************static func declare area****************************
******************************************************************************/
static void NetprintNull(FILE *fp, char *format, ...);
static void show_message_box(GtkWidget *parent, gchar *message, gboolean quit);
static void get_ap_mac(NetWorkWifi *wifi);
static void get_interface_statistics_data(WIFI_INTERFACE *iface);
static void set_title_label(gchar *text, NetWorkWifi *wifi);
static void review_tree_vanpro(char *data, NetWorkWifi *wifi);
static void connet(GtkWidget *widget, NetWorkWifi *wifi);
static void show_connect_info(gchar *info, NetWorkWifi *wifi);
static void read_interface_info(WIFI_INTERFACE *iface);
static void wifi_show_menu_dialog(NetWorkWifi *wifi);

/******************************************************************************
*******************************Func realize eara******************************
******************************************************************************/

/****************** static func realize eara ******************/
static void NetprintNull(FILE *fp, char *format, ...)
{
    return;
}

gboolean get_interface(NetWorkWifi *wifi)
{
#ifdef WEP_CHECK
    FILE *fd;
	gchar buffer[256];
    
    wifi->my_wifi->interface_count = 0 ;

	fd = fopen("/proc/net/dev", "r");

	while(fgets(buffer, 255, fd))
	{
		if(g_strrstr(buffer, "eth1"))
		{
			if(wifi->my_wifi->wifi_interface[0]==NULL)
               			 wifi->my_wifi->wifi_interface[0]=(WIFI_INTERFACE *)g_malloc0(sizeof(WIFI_INTERFACE)+1);
			strncpy(wifi->my_wifi->wifi_interface[0]->name,"eth1",sizeof(WIFI_INTERFACE));
			wifi->my_wifi->interface_count++;
			printf("interface name = %s\n", wifi->my_wifi->wifi_interface[0]->name);
			return TRUE;
		}
	}
	fclose(fd);
	return FALSE;
#else
    FILE *pipe;
    gchar *cmd;
    gchar *search_prefix;
    gchar **tmpline;
    gchar buffer[512];
    gchar *tmp;
    gint index;
    
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }

    DBG("get interface start!!!\n");
    //printf("cell cnt = %d, interface cnt = %d\n", wifi->my_wifi->cell_count, wifi->my_wifi->interface_count);

    cmd=g_strdup_printf("cat /proc/net/wireless");	
    search_prefix=g_strdup(" face");
    pipe = popen (cmd, "r");
    if(!pipe){perror("pipe:");return FALSE;}
    wifi->my_wifi->interface_count=0;
    wifi->my_wifi->interface_index=0;
    index=0;
    if(pipe){
        while ((feof(pipe) == 0)){
             fgets (buffer, 255, pipe);             
             //printf("get buf: %s \n", buffer);
             if (g_str_has_prefix(buffer,search_prefix)){
                 while (feof(pipe)==0){ 
                    printf("%s \n", __FUNCTION__);
                    fgets(buffer,255, pipe);
                    tmpline=g_strsplit(buffer,":",512);
                    tmp=g_strstrip(tmpline[0]);
		    if(wifi->my_wifi->wifi_interface[index]==NULL)
                    wifi->my_wifi->wifi_interface[index]=(WIFI_INTERFACE *)g_malloc0(sizeof(WIFI_INTERFACE));
                    //memset(my_wifi->wifi_interface[index],0,sizeof(WIFI_INTERFACE));
                    strcpy(wifi->my_wifi->wifi_interface[index]->name,tmp);
                    index++;
                    wifi->my_wifi->interface_count++;
                 }                 
             }
        }
        wifi->my_wifi->interface_count--;
    }
    pclose(pipe);
    return FALSE;
#endif
}

gchar *get_string_value(const char *src,const char *target,int unit)
{
	gchar *tmp,*ret;
	gchar **tmpline;
	if(tmp=strstr(src,target))
	{
		tmp+=strlen(target);
		g_strstrip(tmp);
		tmpline=g_strsplit(tmp," ",512);
		if(tmpline){			
			if(unit&&tmpline[1])
			{
				ret=g_strdup_printf("%s %s",tmpline[0],tmpline[1]);
			}
			else
				ret=g_strdup(tmpline[0]);
			g_strfreev(tmpline);	
		}
		
	}
	else
		ret=NULL;
// 	return g_strstrip(ret);
	return ret;

}

gchar *get_essid_value(const char *src, const char *target, int unit)
{
        gchar *tmp,*ret;
        gchar **tmpline;
        if(tmp=strstr(src,target))
        {
                tmp+=strlen(target);
                g_strstrip(tmp);
                tmpline=g_strsplit(tmp,"        ",512);
                if(tmpline)
                {
                        if(unit&&tmpline[1])
                        {
                                ret=g_strdup_printf("%s %s",tmpline[0],tmpline[1]);
                        }
                        else
                                ret=g_strdup(tmpline[0]);
                        g_strfreev(tmpline);   
                }

        }
        else
                ret=NULL;
//      return g_strstrip(ret);
        return ret;

}

void save_config_to_file(gchar *iface, gchar *config)
{
    FILE *fd;
    FILE *fp;

    if (config == NULL)
    {
        return;
    }
    
    remove(WIRELESS_LAST_CONF);
    remove(NETWORK_CONFIG);
    fd = fopen(WIRELESS_LAST_CONF, "w");
    if (fd == NULL)
    {
        DBG("open file: %s failed\n", WIRELESS_LAST_CONF);
        return;
    }
    fprintf(fd, "%s\n", config);   
    fclose(fd);

    // open interface
    if ((fp = fopen(NETWORK_CONFIG, "w+")) == NULL)
    {
        printf("cannot open file: %s!\n", NETWORK_CONFIG);
        return;
    }
    fputs(iface, fp);
    fclose(fp);
}

gboolean my_wifi_init(NetWorkWifi *wifi) 
{
    MY_WIFI *my_wifi = NULL;
    
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }

    my_wifi = (MY_WIFI *)g_malloc0(sizeof(MY_WIFI));
    if (my_wifi)
    {        
        my_wifi->cell_index=-1;
        my_wifi->interface_index=-1;        
        wifi->my_wifi = my_wifi;
        
        memset(wifi->Link_AP_MAC, 0, 48);
        wifi->global_build_list = NULL;
        return TRUE;
    }
    
    return FALSE;
}

static gboolean wifi_disconnect(GtkMenuItem *menuitem, NetWorkWifi *wifi)
{
    GtkWidget *button;
    char buffer[128];
    gchar *cmd;
    
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return FALSE;
    }
    printf("this cell is connect, call %s!\n", __func__);

    //cmd=g_strdup_printf("/sbin/xip-ifdown %s", wifi->my_wifi->wifi_interface[0]->name);
    cmd=g_strdup_printf("/bin/net-disconnect %s", wifi->my_wifi->wifi_interface[0]->name);
    system(cmd);
    printf("this cell is connect, cmd: %s!\n", cmd);
    g_free(cmd);
    
    sprintf(buffer, _("Disconnect"));
    review_tree_vanpro(buffer, wifi);
    if (wifi->connect_index != -1)
        wifi->my_wifi->wifi_cell[wifi->connect_index]->linkstate=LINK_DOWN;
    
    wifi->connect_index=-1;
    return FALSE;
}

gboolean do_disconnect(NetWorkWifi *wifi)
{
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return FALSE;
    }
    return wifi_disconnect(NULL, wifi);
}

static void create_editable_entry(GtkWidget *attach_to,
                                   gchar *wprefix, gchar *ltext, gchar *wdata,
                                   gchar * tooltip, gint clnr, NetWorkWifi *wifi)
{
	GtkWidget *label;
    GtkWidget *entry;
	guint gpe_boxspacing = 0;

    // check param
    if (wifi == NULL)
    {
        return;
    }
    
	label = gtk_label_new(ltext);
	gtk_tooltips_set_tip(wifi->tooltips, label, tooltip, NULL);
	gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
	gtk_table_attach(GTK_TABLE(attach_to), label, 0, 1, clnr, clnr + 1,
        			  (GtkAttachOptions)(GTK_FILL), (GtkAttachOptions)(GTK_FILL),
        			  gpe_boxspacing, gpe_boxspacing);
    
	entry = gtk_entry_new();
	gtk_tooltips_set_tip(wifi->tooltips, entry, tooltip, NULL);
    g_object_set_data(G_OBJECT(wifi->mainwindow), wprefix, entry);
	gtk_entry_set_text(GTK_ENTRY(entry), wdata);
	gtk_entry_set_editable(GTK_ENTRY(entry), TRUE);
	gtk_table_attach(GTK_TABLE(attach_to), entry, 1, 3, clnr, clnr + 1,
    			  (GtkAttachOptions)(GTK_FILL | GTK_EXPAND), (GtkAttachOptions)(GTK_FILL),
    			  gpe_boxspacing, gpe_boxspacing);
}

static void static_entry_status(gboolean flag, NetWorkWifi *wifi)
{
    GtkWidget *entry;
    printf("call %s\n", __func__);

    // check param
    if (NULL == wifi)
    {
        return;
    }
    
    entry = g_object_get_data(G_OBJECT(wifi->mainwindow), "address");
    if (entry) {
        gtk_widget_set_sensitive(entry, flag);
        if (flag)
        {
            gtk_window_set_focus(GTK_WINDOW(wifi->mainwindow), entry);
        }
    }

    entry = g_object_get_data(G_OBJECT(wifi->mainwindow), "netmask");
    if (entry) {
        gtk_widget_set_sensitive(entry, flag);
    }

    entry = g_object_get_data(G_OBJECT(wifi->mainwindow), "gateway");
    if (entry) {
        gtk_widget_set_sensitive(entry, flag);
    }
}

static void set_focus_netmask(GtkEntry *widget, NetWorkWifi *wifi)
{
    GtkWidget *entry;

    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s\n", __FUNCTION__);
        return;
    }
    
    entry = g_object_get_data(G_OBJECT(wifi->mainwindow), "netmask");
    gtk_window_set_focus(GTK_WINDOW(wifi->mainwindow), entry);
}

static void set_focus_gateway(GtkEntry *widget, NetWorkWifi *wifi)
{
    GtkWidget *entry;

    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s\n", __FUNCTION__);
        return;
    }
    
    entry = g_object_get_data(G_OBJECT(wifi->mainwindow), "gateway");
    gtk_window_set_focus(GTK_WINDOW(wifi->mainwindow), entry);
}

static void set_focus_nameserver(GtkEntry *widget, NetWorkWifi *wifi)
{
    GtkWidget *entry;

    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s\n", __FUNCTION__);
        return;
    }
    
    entry = g_object_get_data(G_OBJECT(wifi->mainwindow), "nameserver");
    gtk_window_set_focus(GTK_WINDOW(wifi->mainwindow), entry);
}

static void set_focus_nameserver2(GtkEntry *widget, NetWorkWifi *wifi)
{
    GtkWidget *entry;

    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s\n", __FUNCTION__);
        return;
    }
    
    entry = g_object_get_data(G_OBJECT(wifi->mainwindow), "nameserver2");
    gtk_window_set_focus(GTK_WINDOW(wifi->mainwindow), entry);
}

static gint get_first_char(gchar *s)
{
	gint i = 0;

    if (s == NULL)
    {
        return -1;
    }
	for (i=0; i<strlen(s); i++)
		if ((s[i] != ' ') && (s[i] != '\t') && (s[i] != '\n') && (s[i] != '\0')) return i;
	return -1;
}

static gint get_param_val(gchar* line, gchar* param, gchar* value)
{
    gint a = 0;
    gint b = 0;
    gint st = 0;
    gint sep = 0;
	
    st = get_first_char(line);
    if (line[st] == '#') return -1; // mark comment 
    for (sep=st; sep<strlen(line); sep++)
        if (line[sep]==' ') break;
    for (a=sep; a<strlen(line); a++)
        if ((line[a]!=' ') && (line[a]!='\t')) break;
    for (b=a+1; b<strlen(line); b++)
        if ((line[b]=='\n') || (line[b]=='\0')) break;
    param = strncpy(param,&line[st],sep-st);
    param[sep-st] = '\0';

    if ((b-a) > 0) value = strncpy(value,&line[a],b-a);	
    value[b-a]='\0';
    return 0;
}

static void read_interface_info(WIFI_INTERFACE *iface)
{
    gchar *filename;
    FILE *fd;
    char buffer[256];
    gchar paramval[256];
    gchar ifname[256];
    gchar *ret;
    gint index = 0;

    filename = g_strdup_printf("%sif_%s", NET_CONFIG_FOLDER, iface->name);    
    if (!access(filename, 4))
    {
        printf("Access file success:%s\n", filename);
        fd = fopen(filename, "r");
        if (fd)
        {
            do
            {
                ret = fgets(buffer, 255, fd);
                if (get_param_val(buffer, paramval, ifname) == -1) 
                {
                    printf("call %s failed!\n");
                    memset(buffer, 0, 256);
                    continue;  
                } 
                if (!strcmp("iface", paramval))
                {
                    if (!strcmp(paramval, iface->name))
                    {
                        printf("Worng config file.\n");
                        fclose(fd);
                        return;
                    }
                }
                else if (!strcmp("address", paramval))
                {
                    strcpy(iface->address, g_strstrip(ifname));
                }
                else if (!strcmp("netmask", paramval))
                {
                    strcpy(iface->netmask,g_strstrip(ifname));
                }
                else if(!strcmp("gateway", paramval))
                {
                    strcpy(iface->gateway, g_strstrip(ifname));
                }
                else if(!strcmp("nameserver", paramval))
                {
                    iface->nameserver[index] = g_strdup(ifname);
                    index++;
                }
                memset(buffer, 0, 256);
            }while(ret!=NULL);
            fclose(fd);
        }
    }
}

static void wirte_interface_info(WIFI_INTERFACE *iface)
{
    FILE *fd = NULL;
    gchar *ret = NULL;
    gchar *filename = NULL;
    gchar ifname[256] = {0};
    gchar buffer[256] = {0};
    gchar paramval[256] = {0};

    // check param
    if (NULL == iface)
    {
        fprintf(stderr, "input param invalid in %s\n", __func__);
        return;
    }

    filename = g_strdup_printf("%sif_%s", NET_CONFIG_FOLDER, iface->name);    
    remove(filename);
    fd = fopen(filename, "w");
    if (fd)
    {
        if (iface->name)
        {
            fprintf(fd, "iface %s\n", iface->name);
        }      
        if (iface->isstatic)
        {
            fprintf(fd, "addrtype static\n");
        }
        else
        {
            fprintf(fd, "addrtype dhcp\n");
        }        
        if (iface->address)
        {
            fprintf(fd, "address %s\n", iface->address);
        }
        if (iface->netmask)
        {
            fprintf(fd, "netmask %s\n", iface->netmask);
        }
        if (iface->gateway)
        {
            fprintf(fd, "gateway %s\n", iface->gateway);
        }
        if (iface->nameserver[0])
        {
            fprintf(fd, "nameserver %s\n", iface->nameserver[0]);
        }
        else
        {
            fprintf(fd, "nameserver \n");
        }
        if (iface->nameserver[1])
        {
            fprintf(fd, "nameserver %s\n", iface->nameserver[1]);
        }
    }
    fclose(fd);
}

static gboolean address_is_valid(gchar *ip){
    if(ip==NULL)return FALSE;
    struct sockaddr_in addr;
    if (inet_pton(AF_INET, ip, &addr.sin_addr)==0){
        perror("inet_pton");
        printf("invalid: (%s), len: (%d)\n", ip, strlen(ip));
        return FALSE;
    }
    return TRUE;
}

static void save_nameserver(NetWorkWifi *wifi)
{
    gchar *name1 = NULL;
    gchar *name2 = NULL;
    gboolean changed = FALSE;
    GtkWidget *entry;

    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s\n", __func__);
        return;
    }
    
	// save nameserver
    FILE *fd = fopen("/etc/resolv.conf", "w");     
    if (!fd)
    {
        printf("file /etc/resolv.conf cann't access\n");
    }
#if 0
	entry = g_object_get_data(G_OBJECT(wifi->mainwindow), "nameserver");
	if (entry)
	{
		name1 = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        if (strlen(name1) > 0)
        {
            if (address_is_valid(name1))
            {
                wifi->my_wifi->wifi_interface[0]->nameserver[0] = g_strdup(name1);
                fprintf(fd, "nameserver %s\n", name1);
            }
            else
            {
                show_message_box(wifi->mainwindow, _("nameserver option invalid"), FALSE);  
                fclose(fd);
                return;
            }
        }
	}

	entry = g_object_get_data(G_OBJECT(wifi->mainwindow), "nameserver2");
	if (entry)
	{
		name2 = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        if (strlen(name2)>0)
        {
            if (address_is_valid(name2))
            {
                wifi->my_wifi->wifi_interface[0]->nameserver[1] = g_strdup(name2);
                fprintf(fd, "nameserver %s\n", name2);
            }
            else
            {
                show_message_box(wifi->mainwindow, _("nameserver option invalid"), FALSE);        
                fclose(fd);
                return;
            }
        }
	}
#endif
    if (wifi->my_wifi->wifi_interface[0]->nameserver[0])
    {
        fprintf(fd, "nameserver %s\n", wifi->my_wifi->wifi_interface[0]->nameserver[0]);
    }
    if (wifi->my_wifi->wifi_interface[0]->nameserver[1])
    {
        fprintf(fd, "nameserver %s\n", wifi->my_wifi->wifi_interface[0]->nameserver[1]);
    }

    fclose(fd);
    return;
}

static void set_staticip(GtkWidget* widget, NetWorkWifi *wifi)
{
	GtkWidget *entry;
    gchar *name1 = NULL;
    gchar *name2 = NULL;
	gchar *newval = NULL;
    gchar *ipinfo = NULL;

    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s\n", __FUNCTION__);
        return;
    }
    
    if (wifi->my_wifi->wifi_interface[0]->nameserver[0] != NULL)
    {
        g_free(wifi->my_wifi->wifi_interface[0]->nameserver[0]);
        wifi->my_wifi->wifi_interface[0]->nameserver[0] = NULL;
    }
    if (wifi->my_wifi->wifi_interface[0]->nameserver[1] != NULL)
    {
        g_free(wifi->my_wifi->wifi_interface[0]->nameserver[1]);
        wifi->my_wifi->wifi_interface[0]->nameserver[1] = NULL;
    }
    
    wifi->my_wifi->wifi_interface[0]->isstatic = TRUE;
    wifi->my_wifi->wifi_interface[0]->isdhcp = FALSE;
    entry = g_object_get_data(G_OBJECT(wifi->mainwindow), "address");
    if (entry) 
    {
        newval = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        if (address_is_valid(newval))
        {
            strcpy(wifi->my_wifi->wifi_interface[0]->address, newval);
        }
        else
        {
            ipinfo=g_strdup(_("Input Address is invalid"));
            show_message_box(wifi->mainwindow, ipinfo, FALSE);
            return;
        }
    }

    entry = g_object_get_data(G_OBJECT(wifi->mainwindow), "netmask");
    if (entry) 
    {
        newval = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        if (address_is_valid(newval))
        {
            strcpy(wifi->my_wifi->wifi_interface[0]->netmask, newval);
        }
        else
        {
            ipinfo=g_strdup(_("Input Netmask is invalid"));
            show_message_box(wifi->mainwindow, ipinfo, FALSE); 
            return;
        }
    }

    entry = g_object_get_data(G_OBJECT(wifi->mainwindow), "gateway");
    if (entry)
    {
        newval = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        if (address_is_valid(newval))
        {
            strcpy(wifi->my_wifi->wifi_interface[0]->gateway, newval);
        }
        else
        {
            ipinfo=g_strdup(_("Input Gateway is invalid"));
            show_message_box(wifi->mainwindow, ipinfo, FALSE);
            return;
        }
    }
    entry = g_object_get_data(G_OBJECT(wifi->mainwindow), "nameserver");
    if (entry)
    {
        name1 = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        if (strlen(name1) > 7)
        {
            if (address_is_valid(name1))
            {
                wifi->my_wifi->wifi_interface[0]->nameserver[0] = g_strdup(name1);
            }
            else
            {
                printf("show nameserver option invalid 1: %s, len = %d\n", name1, strlen(name1));
                show_message_box(wifi->mainwindow, _("nameserver option invalid"), FALSE);  
                return;
            }
        }
    }

    entry = g_object_get_data(G_OBJECT(wifi->mainwindow), "nameserver2");
    if (entry)
    {
        name2 = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        if (strlen(name2)>7)
        {
            if (address_is_valid(name2))
            {
                wifi->my_wifi->wifi_interface[0]->nameserver[1] = g_strdup(name2);
            }
            else
            {
                printf("show nameserver option invalid 2: %s, len = %d\n", name2, strlen(name2));
                show_message_box(wifi->mainwindow, _("nameserver option invalid"), FALSE);        
                return;
            }
        }
    }

    save_nameserver(wifi);
    printf("wirte_interface_info, dhcp: %d\n", wifi->my_wifi->wifi_interface[0]->isdhcp);
    wirte_interface_info(wifi->my_wifi->wifi_interface[0]);
    //printf("write interface info: isdhcp: %d, ip: %s, netmask: %s, gateway: %s, nameserver1: %s, nameserver2: %s\n", 
    //        lan->iface.isdhcp, lan->iface.address, lan->iface.netmask, lan->iface.gateway, lan->iface.nameserver[1], lan->iface.nameserver[2]);
    GtkWidget *dialog = g_object_get_data(G_OBJECT(wifi->mainwindow), "ip_dlg");
    if (dialog)
    {
        gtk_widget_destroy(dialog);
    }
}

static GtkWidget* create_staticip_table(NetWorkWifi *wifi)//Iface_t iface
{
	GtkWidget *ctable;
    GtkWidget *entry;
	guint gpe_boxspacing = 2;

    // check param
    if (NULL == wifi)
    {
        fprintf(stderr, "input param invalid in %s\n", __func__);
        return;
    }
    
    WIFI_INTERFACE *iface = wifi->my_wifi->wifi_interface[0];
	ctable = gtk_table_new(16, 3, FALSE);
	gtk_table_set_col_spacings(GTK_TABLE(ctable), gpe_boxspacing);
	gtk_table_set_row_spacings(GTK_TABLE(ctable), gpe_boxspacing);
    GtkWidget *label = gtk_label_new(_("enter following info to set ip manually,\nesc or cancel to get ip automatically."));
    gtk_table_attach(GTK_TABLE(ctable), label, 0, 3, 0, 1,
                  (GtkAttachOptions)(GTK_FILL | GTK_EXPAND), (GtkAttachOptions)(GTK_FILL), 0, 0);

	wifi->tooltips = gtk_tooltips_new();
    create_editable_entry(ctable, "address", _("Address"), iface->address,  
                            _("Enter your IP address here, e.g. 192.168.1.2"), 1, wifi);  
    entry=g_object_get_data(G_OBJECT(wifi->mainwindow), "address");
    g_signal_connect(G_OBJECT(entry), "activate", G_CALLBACK(set_focus_netmask), wifi);
    
    create_editable_entry (ctable, "netmask", _("Netmask"), iface->netmask, 
                            _("Enter your netmask here, e.g. 255.255.255.0 in most cases"), 2, wifi);
    entry=g_object_get_data(G_OBJECT(wifi->mainwindow), "netmask");
    g_signal_connect(G_OBJECT(entry), "activate", G_CALLBACK(set_focus_gateway), wifi);

   
    create_editable_entry(ctable, "gateway", _("Gateway"), iface->gateway,
                            _("Enter the IP Address of your default gateway here."), 3, wifi);  
    entry=g_object_get_data(G_OBJECT(wifi->mainwindow), "gateway");
    //g_signal_connect(G_OBJECT(entry), "activate", G_CALLBACK(set_focus_nameserver), wifi);
    g_signal_connect(G_OBJECT(entry), "activate", G_CALLBACK(set_focus_nameserver), wifi);

    // create dns radio button
	create_editable_entry(ctable, "nameserver", _("Primary DNS"), iface->nameserver[0],
				       _("Enter the Primary DNS(name-) server to use here."), 4, wifi);// server
    entry=g_object_get_data(G_OBJECT(wifi->mainwindow), "nameserver");
    g_signal_connect(G_OBJECT(entry), "activate", G_CALLBACK(set_focus_nameserver2), wifi);
    
    
	create_editable_entry(ctable, "nameserver2", _("Secondary DNS"), iface->nameserver[1],
				            _("Enter the Secondary DNS(name-) server to use here."), 5, wifi);
    entry=g_object_get_data(G_OBJECT(wifi->mainwindow), "nameserver2");
    g_signal_connect(G_OBJECT(entry), "activate", G_CALLBACK(set_staticip), wifi);
    return ctable;
}

static gint ip_esc_press(GtkWidget *widget, GdkEventButton *event, NetWorkWifi *wifi)
{
    GdkEventKey *key;
    
    key = (GdkEventKey *)event;
    if (key->keyval != GDK_Escape)
    {
        return FALSE;
    }

    if (NULL == wifi)
    {
        return FALSE;
    }
    wifi->my_wifi->wifi_interface[0]->isstatic = FALSE;
    wifi->my_wifi->wifi_interface[0]->isdhcp = TRUE;
    wirte_interface_info(wifi->my_wifi->wifi_interface[0]);
    gtk_widget_destroy(widget);
    return TRUE;
}

static GtkWidget* create_staticip_dlg(NetWorkWifi *wifi)
{
    GtkWidget *dialog;
    GtkWidget *ctable;
    GtkWidget *btnok;

    // check param
    if (NULL == wifi)
    {
        return;
    }
    
    gtk_rc_parse_string( "style \"font-default\"" \
                         "{font_name=\"Sans 10\"}" \
                         "class \"*\" style \"font-default\"" );
    read_interface_info(wifi->my_wifi->wifi_interface[0]);
    dialog = gtk_dialog_new_with_buttons(_("ip config"),
                                        GTK_WINDOW(wifi->mainwindow),
                                        GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
                                        GTK_STOCK_OK,
                                        GTK_RESPONSE_OK,
                                        NULL);

    /* set window parameters depending on screen size */
    gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
    gtk_window_set_default_size(GTK_WINDOW(dialog),360,320);
    btnok = gtk_dialog_add_button(GTK_DIALOG(dialog),
                                GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT );
    GTK_WIDGET_SET_FLAGS(btnok, GTK_CAN_DEFAULT);
    gtk_widget_grab_default(btnok);
    
    gtk_widget_add_events(GTK_WIDGET(dialog), GDK_KEY_PRESS_MASK);
    gtk_signal_connect(GTK_OBJECT(dialog), "key-press-event",GTK_SIGNAL_FUNC(ip_esc_press),wifi);
    
    // page headers
    ctable = create_staticip_table(wifi);
    gtk_container_set_border_width(GTK_CONTAINER(ctable), 5);
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), ctable, TRUE, TRUE, 0);
    return dialog;
}

static gboolean connect_key_cb(GdkEventKey *key_event, NetWorkWifi *wifi)
{
	GtkWidget * widget;
	
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }

	widget = g_object_get_data(G_OBJECT(wifi->mainwindow), "connect_key");
	if(!gtk_entry_get_visibility(GTK_ENTRY(widget)))
		gtk_entry_set_text(GTK_ENTRY(widget), " ");

	return FALSE;
}

void cb_key_index_stat(GtkWidget *combobox,NetWorkWifi *wifi)
{
    gint index;
    GtkWidget *keycombox;

    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }
    
    index = gtk_combo_box_get_active(GTK_COMBO_BOX(combobox));
    keycombox = g_object_get_data(G_OBJECT(wifi->mainwindow), "Key_Index_ComboBox");
    if(index)
        gtk_widget_set_sensitive(keycombox, FALSE);
    else
        gtk_widget_set_sensitive(keycombox, TRUE);
}

//shuai changed
GtkWidget *create_Authentication_ComboBox(NetWorkWifi *wifi)
{
	GtkWidget *backend_list;
	GtkWidget *combobox;
	GtkListStore *store;
	GtkTreeIter iter;
	GtkCellRenderer *renderer;
	gchar *wdata;

    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }

	store=gtk_list_store_new(1,G_TYPE_STRING); 
	gtk_list_store_append(store, &iter);
	gtk_list_store_set(store, &iter, 0,"WEP", -1);
	
	gtk_list_store_append(store, &iter);
	gtk_list_store_set(store, &iter, 0,"WPA-PSK/WPA2-PSK", -1);
	
	combobox=gtk_combo_box_new_with_model((GtkTreeModel*)store);
	gtk_object_set_data(GTK_OBJECT(wifi->mainwindow), "Authentication_ComboBox" , combobox);
    gtk_combo_box_set_active(GTK_COMBO_BOX(combobox),0);
        
#ifdef WEP_CHECK
    GtkTreeIter iter1;
    GtkTreeModel *model;
    GtkTreeSelection *selection;
    GtkWidget *treeview = g_object_get_data(G_OBJECT(wifi->mainwindow), "view-press");
    model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
    if (!gtk_tree_selection_get_selected(selection, (GtkTreeModel **)&model, &iter1))
    {
        DBG("there is no selected video, can't into list!\n");
        return;
    }
    
    gchar *type = NULL;
    gtk_tree_model_get(model, &iter1, COL_KEY, &type, -1);
    printf("___get type : %s\n", type);
    //if(g_str_has_prefix(wifi->my_wifi->wifi_cell[wifi->connect_index]->encryption_type, "WEP"))
    if(g_str_has_prefix(type, "WEP"))
    {
        gtk_combo_box_set_active(GTK_COMBO_BOX(combobox),0);
    }
    else
    {
        gtk_combo_box_set_active(GTK_COMBO_BOX(combobox),1);
    }
#endif
	renderer = gtk_cell_renderer_text_new();
	gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combobox),renderer,FALSE);
	gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combobox), renderer,"text", 0,NULL);
    g_signal_connect(G_OBJECT(combobox),"changed",G_CALLBACK(cb_key_index_stat),wifi);
	return combobox;
}

static void set_focus(GtkWidget* widget, NetWorkWifi *wifi)
{
    GtkWidget *entry;
    
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s\n", __FUNCTION__);
        return;
    }
    
    entry = g_object_get_data(G_OBJECT(wifi->mainwindow), "key_index");
    gtk_window_set_focus(GTK_WINDOW(wifi->mainwindow), entry);
}

static GtkWidget* create_ipconfig_dlg(NetWorkWifi *wifi)
{
    gint result;
    GtkWidget *msgdlg;

    // check param
    if (NULL == wifi)
    {
        fprintf(stderr, "input param invalid in %s\n", __func__);
        return NULL;
    }
    
    gtk_rc_parse_string( "style \"font-default\"" \
                         "{font_name=\"Sans 9\"}" \
                         "class \"*\" style \"font-default\"" );
    // create a message dialog
    msgdlg = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_NONE,
        _("Please select a lan connect mode, use dhcp get ip automatically or set static ip manually."));
	g_signal_connect(G_OBJECT(msgdlg), "delete-event",
                	G_CALLBACK(gtk_widget_destroy), NULL);

    gtk_dialog_add_buttons(GTK_DIALOG(msgdlg), _("DHCP"), RESPONSE_DHCP,
                                   _("Static IP"), RESPONSE_STATIC, NULL);
    gtk_widget_add_events(GTK_WIDGET(msgdlg), GDK_KEY_PRESS_MASK);
    gtk_signal_connect(GTK_OBJECT(msgdlg), "key-press-event",GTK_SIGNAL_FUNC(ip_esc_press),wifi);
    return msgdlg;
}

static GtkWidget* show_info(GtkWidget *cb_widget, NetWorkWifi *wifi)
{
    gint result;
    GtkWidget *msgdlg;
    GtkWidget *treeview;
    GtkTreeSelection *selection;
    GtkTreeModel *model;
    GtkTreeIter iter;

    // check param
    if (NULL == wifi)
    {
        fprintf(stderr, "input param invalid in %s\n", __func__);
        return NULL;
    }
    
    gtk_rc_parse_string( "style \"font-default\"" \
                         "{font_name=\"Sans 10\"}" \
                         "class \"*\" style \"font-default\"" );

#if 0
    gchar *essid = NULL;
    gchar *mode = NULL;
    gchar *type = NULL;
    gchar *signal = NULL;
    gchar *state = NULL;

    treeview = g_object_get_data(G_OBJECT(wifi->mainwindow), "view-press");
    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
    model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
    if (!gtk_tree_selection_get_selected(selection, (GtkTreeModel **)&model, &iter))
    {
        DBG("there is no selected signal, can't connect!\n");
        return;
    }

    gtk_tree_model_get(model, &iter, 
                    COL_ESSID, &essid, 
                    COL_MODE, &mode, 
                    COL_KEY, &type, 
                    COL_QUALITY, &signal, 
                    COL_STATE, &state, -1);
    
    printf("get cell info: essid: %s, mode: %s, key: %s, signal: %s, state: %s\n", essid, mode, type, signal, state);
    gchar *message = NULL;
    if (strcmp(state, "Connected") == 0)
    {
        message = g_strdup_printf("essid: %s\nmode: %s\ntype: %s\nsignal: %s\nstate: %s\nMAC: %s\nIP Address: %s\n",
                                    essid, mode, type, signal, state, wifi->Link_AP_MAC, wifi->my_wifi->wifi_interface[0]->address);
    }
    else
    {
        message = g_strdup_printf("essid: %s\nmode: %s\ntype: %s\nsignal: %s\nstate: %s\n",
                                    essid, mode, type, signal, state);
    }
#else
    printf("wifi connect index = %d\n", wifi->connect_index);
    WIFI_CELL *wifi_cell = NULL;
    wifi_cell = wifi->my_wifi->wifi_cell[wifi->connect_index];
    gchar *message = NULL;
    if (wifi->connect_index >= 0)
    {
        if (strcmp(wifi_cell->state, "Connected") == 0)
        {
            message = g_strdup_printf("state: %s\nessid: %s\nmode: %s\ntype: %s\nsignal: %s\nMAC: %s\nIP Address: %s",
                                    wifi_cell->state, wifi_cell->essid, wifi_cell->mode, wifi_cell->encryption_type, 
                                     wifi_cell->quality, wifi->Link_AP_MAC, wifi->my_wifi->wifi_interface[0]->address);
        }
        else
        {
            message = g_strdup_printf("state: %s\nessid: %s\nmode: %s\ntype: %s\nsignal: %s",
                                    wifi_cell->state, wifi_cell->essid, wifi_cell->mode, wifi_cell->encryption_type, wifi_cell->quality);
        }
    }
    else
    {
        message = g_strdup_printf("state: not connect");
    }
#endif

    msgdlg = gtk_message_dialog_new(NULL,
                                    GTK_DIALOG_MODAL,
                                    GTK_MESSAGE_INFO,
                                    GTK_BUTTONS_CLOSE,
                                    message);
    gtk_widget_show_all(msgdlg);
    gtk_dialog_run(GTK_DIALOG(msgdlg));
    gtk_widget_destroy(msgdlg);
    return msgdlg;
}

GtkWidget * build_encrypted_dialog(NetWorkWifi *wifi)
{
    GtkWidget *dialog,*ctable,*label,*frame,*ftable,*widget;
    GtkWidget *combo;
    gchar wname[80];
    gint index;
    GtkWidget *pwentry;
    GtkWidget *keyentry;
    GtkWidget *hbox;
    
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }

    dialog = gtk_dialog_new_with_buttons(_("Encrypted Network"),
                                        NULL,
                                        GTK_DIALOG_DESTROY_WITH_PARENT,
                                        GTK_STOCK_OK,
                                        GTK_RESPONSE_OK,
                                        GTK_STOCK_CANCEL,
                                        GTK_RESPONSE_REJECT,
                                        NULL);
    
    /* set window parameters depending on screen size */
    //gtk_window_set_default_size(GTK_WINDOW(dialog),500,320);
    gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, TRUE);
    GTK_WIDGET_SET_FLAGS(dialog, GTK_CAN_DEFAULT);
    gtk_widget_grab_default(dialog);
    
    ctable = gtk_table_new(5, 2, FALSE);
    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), ctable, TRUE, TRUE, 0);
    gtk_container_set_border_width(GTK_CONTAINER(ctable), 5);
    
    label=gtk_image_new_from_stock(GTK_STOCK_DIALOG_AUTHENTICATION, GTK_ICON_SIZE_DIALOG);
    gtk_table_attach(GTK_TABLE(ctable), label, 0, 1, 0, 1,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (GTK_FILL),
                    5, 5);
    
    label = gtk_label_new(_("Encrypted Network, Please enter the key."));
    gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
    gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
    gtk_table_attach(GTK_TABLE(ctable), label, 1, 2, 0, 1,
                    (GtkAttachOptions) (GTK_FILL)|GTK_EXPAND,
                    (GtkAttachOptions) (GTK_FILL)|GTK_EXPAND,
                    5, 5);

	hbox = gtk_hbox_new(FALSE, 0);//10
	gtk_table_attach(GTK_TABLE(ctable), hbox, 0, 2, 1, 2,
					 (GtkAttachOptions) (GTK_FILL)|(GtkAttachOptions)GTK_EXPAND,
					  (GtkAttachOptions) (GTK_FILL)|(GtkAttachOptions)GTK_EXPAND,
					   5, 5); //10
    ftable=gtk_table_new(1,2,TRUE);
    gtk_container_set_border_width(GTK_CONTAINER(ftable), 5);
    gtk_container_add(GTK_CONTAINER(hbox), ftable);

    label = gtk_label_new(_("PassWord: "));
	gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
    gtk_table_attach_defaults(GTK_TABLE(ftable), label, 0, 1, 0, 1); 
    pwentry = gtk_entry_new();                              
    strcpy(wname, "connect_key");
    gtk_widget_set_name(GTK_WIDGET(pwentry), wname);
    //gtk_widget_set_size_request(pwentry, 225, 45);//30
    gtk_object_set_data(GTK_OBJECT(wifi->mainwindow), wname , pwentry);
    gtk_entry_set_editable(GTK_ENTRY(pwentry), TRUE);
    gtk_table_attach_defaults(GTK_TABLE(ftable), pwentry, 1, 2, 0, 1); 

    
	hbox = gtk_hbox_new(FALSE, 0);
    gtk_table_attach(GTK_TABLE(ctable), hbox, 0, 2, 3, 4,
                    (GtkAttachOptions) (GTK_FILL)|(GtkAttachOptions)GTK_EXPAND,
                    (GtkAttachOptions) (GTK_FILL)|(GtkAttachOptions)GTK_EXPAND,
                    5, 5);  

    ftable=gtk_table_new(1,2,TRUE);
    gtk_container_set_border_width(GTK_CONTAINER(ftable), 5);
    gtk_container_add(GTK_CONTAINER(hbox), ftable);
    
    label = gtk_label_new(_("Key Index(1-4): "));
	gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
    gtk_table_attach_defaults(GTK_TABLE(ftable), label, 0, 1, 0, 1); 
    keyentry = gtk_entry_new();                              
    strcpy(wname, "key_index");
    gtk_widget_set_name(GTK_WIDGET(keyentry), wname);
    //gtk_widget_set_size_request(keyentry, 220, 45);//30
    gtk_entry_set_text(GTK_ENTRY(keyentry), "1");
    gtk_object_set_data(GTK_OBJECT(wifi->mainwindow), wname , keyentry);
    gtk_entry_set_editable(GTK_ENTRY(keyentry), TRUE);
    gtk_table_attach_defaults(GTK_TABLE(ftable), keyentry, 1, 2, 0, 1); 
#ifdef WEP_CHECK
    if(g_str_has_prefix(wifi->type, "WEP"))
    {
        gtk_widget_set_sensitive(label, TRUE);
        gtk_widget_set_sensitive(keyentry, TRUE);
        g_signal_connect(G_OBJECT(keyentry), "activate",
                         G_CALLBACK(connet), wifi);
        g_signal_connect(G_OBJECT(pwentry), "activate",
                         G_CALLBACK(set_focus), wifi);
    }
    else
    {
        gtk_widget_set_sensitive(label, FALSE);
        gtk_widget_set_sensitive(keyentry, FALSE);
        g_signal_connect(G_OBJECT(pwentry), "activate",
                         G_CALLBACK(connet), wifi);
    }
#else
    g_signal_connect(G_OBJECT(keyentry), "activate",
                     G_CALLBACK(connet), wifi);
#endif
    gtk_window_set_keep_above(GTK_WINDOW(dialog), TRUE);
    return dialog;
}

void apply_configuration(NetWorkWifi *wifi)
{
    gchar *cmd;
    gchar *s_enc;
    gchar *s_key;
	gchar *s_mode;
    char essid_buf[256];
    
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }
    if(wifi->my_wifi->interface_count>0){
		
        DBG("essid to set:%s\n",wifi->my_wifi->wifi_cell[wifi->connect_index]->essid);
        DBG("interface:%s\n",wifi->my_wifi->wifi_interface[0]->name);
        
        if(g_str_equal(wifi->my_wifi->wifi_cell[wifi->connect_index]->userkey, "")){
            s_key=g_strdup("off");
        }else{
            if(wifi->my_wifi->wifi_cell[wifi->connect_index]->userkey)
                s_key=g_strdup(wifi->my_wifi->wifi_cell[wifi->connect_index]->userkey);
            else
                s_key=g_strdup("");            
        }
        
        
        if(wifi->my_wifi->wifi_cell[wifi->connect_index]->encmode==ENC_OFF){
            s_enc=g_strdup("off");
        }else if(wifi->my_wifi->wifi_cell[wifi->connect_index]->encmode==ENC_OPEN){
            s_enc=g_strdup("open");            
        }else{
            s_enc=g_strdup("restricted");
        }        
	
        if(strcasecmp(wifi->my_wifi->wifi_cell[wifi->connect_index]->mode,"Master")==0)
    	{
    		s_mode=g_strdup("Managed");
    	}
	else
		s_mode=g_strdup(wifi->my_wifi->wifi_cell[wifi->connect_index]->mode);
    #if 1
        if(wifi->my_wifi->wifi_cell[wifi->connect_index]->key_index == 0)
            wifi->my_wifi->wifi_cell[wifi->connect_index]->key_index = 1;
    #endif
        //if(!g_str_has_prefix(wifi->type, "WEP"))
        if(g_str_has_prefix(wifi->type, "WPA"))
        {
#ifdef WEP_CHECK
            cmd=g_strdup_printf("wpa \"%s\" essid \"%s\" mode \"%s\" key \"%s\" enc \"%s\" dev \"%s\"", 
                    wifi->my_wifi->wifi_interface[0]->name,
                    wifi->my_wifi->wifi_cell[wifi->connect_index]->essid,
                    s_mode,    
                    s_key,
                    s_enc,
                    wifi->my_wifi->wifi_cell[wifi->connect_index]->device_type);
#else
                cmd=g_strdup_printf("wpa \"%s\" essid \"%s\" mode \"%s\" key \"%s\" enc \"%s\"", 
                        wifi->my_wifi->wifi_interface[0]->name,
                        wifi->my_wifi->wifi_cell[wifi->connect_index]->essid,
                        s_mode,    
                        s_key,
                        s_enc);
#endif
                printf("wpa_supplicant cmd=%s\r\n",cmd);
		}
		else
        {
			//cmd=g_strdup_printf("wep \"%s\" essid \"%s\" mode \"%s\" key \"%s\" enc \"%s\" \"[%d]\"", 
            cmd=g_strdup_printf("wep \"%s\" essid \"%s\" mode \"%s\" key \"%s\" enc \"%s\" [%d]", 
					wifi->my_wifi->wifi_interface[0]->name,
					wifi->my_wifi->wifi_cell[wifi->connect_index]->essid,
					s_mode,
					s_key,
					s_enc,
                    wifi->my_wifi->wifi_cell[wifi->connect_index]->key_index);//wifi->my_wifi->wifi_cell[wifi->connect_index]->key_index+1
			printf("iwset cmd=%s\r\n",cmd);
		}
        save_config_to_file(wifi->my_wifi->wifi_interface[0]->name, cmd);
        printf("------save wifi config: %s\n", cmd);
        g_free(cmd);
        //cmd = g_strdup_printf("%s restart", NETWORK_SETUP_SCRIPT);
        cmd = g_strdup_printf("%s connect", NETWORK_SETUP_SCRIPT);
        system(cmd);

        g_free(cmd);
		g_free(s_mode);
		g_free(s_key);
		g_free(s_enc);
    }
}

gboolean require_ip_address(NetWorkWifi *wifi){
    int fPID;
    int cPID;
    int exPID;
    
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }
    
    fPID = fork();
    if(fPID<0){
        //DBG("fork PID %d error %d\n", fPID, errno);
        perror("fork error");
    }
    if(fPID==0){
        cPID=fork();
        if(cPID==0){
            //execl("/sbin/xip-ifup","xip-ifup", wifi->my_wifi->wifi_interface[0]->name,0);
            execl("/bin/net-connect","net-connect", wifi->my_wifi->wifi_interface[0]->name,0);
            exit(0);
        }else if(cPID>0){exit(0);}
    }else{
        exPID=wait(NULL);
        DBG("wait fPID=%d,exPID=%d\n", fPID, exPID);
    }
    return FALSE;
}

gboolean address_valid(WIFI_INTERFACE *iface)
{
    if(!iface)
        return FALSE;
    int inet_sock;
    struct ifreq ifr;
    inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
    strcpy(ifr.ifr_name, iface->name);
    if (ioctl(inet_sock, SIOCGIFADDR, &ifr) < 0){
        close(inet_sock);
        return FALSE;
    }else{
        close(inet_sock);
        return TRUE;
    }
}

gboolean detect_network_interface_state(NetWorkWifi *wifi)
{
    gchar buffer[128];
    GtkWidget *button;
    static int count=0;
    DBG("detect_network_interface_state ...\n");
    
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }

    if(address_valid(wifi->my_wifi->wifi_interface[0])){//if address is valid
        count=0;
        DBG("acquire address successed.\n");
        //send_atom_to_desktop("_NET_WIFI_CHANGE_STATUS",WIFI_STATUS_GETIP);
        sprintf(buffer, _("Wireless network selection"));        
        set_title_label(buffer, wifi);
        get_interface_statistics_data(wifi->my_wifi->wifi_interface[0]);
    #if 0
        sprintf(buffer,"%s:%s",_("Connected\nIP Address"),wifi->my_wifi->wifi_interface[0]->address);
        review_tree_vanpro(buffer, wifi);
    #else
        sprintf(buffer,"%s:%s",_("Connected.\nIP Address"),wifi->my_wifi->wifi_interface[0]->address);
        show_connect_info(buffer, wifi);
        wifi->RUNNING = FALSE;
    #endif
        return FALSE;
    }else{//if address is invalid
        count++;
        if(count>20){
            count=0;
			if(wifi->my_wifi->wifi_interface[0]->wifi_group_enable)
				return FALSE;  
            DBG("acquire address failed.\n");
            sprintf(buffer, _("Invalid IP"));
            review_tree_vanpro(buffer, wifi);   
            show_connect_info(buffer, wifi);
            wifi->RUNNING = FALSE;
            return FALSE;            
        }
    }
    return TRUE;
}

gboolean detect_network_link_state(NetWorkWifi *wifi)
{
    
    static int link_count=0;
    
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }
    
    char buffer[128];
    get_ap_mac(wifi);
    if(g_str_equal(wifi->Link_AP_MAC,"")||g_str_equal(wifi->Link_AP_MAC,"00:00:00:00:00:00")){
        DBG("AP MAC[%s]:Disconnected\n",wifi->Link_AP_MAC);
        link_count++;
        if(link_count>20){
            wifi->linkconnect=FALSE;            
            //send_atom_to_desktop("_NET_WIFI_CHANGE_STATUS",WIFI_STATUS_UP);
            DBG("Not connect,return.\n");
            //sprintf(buffer, _("Not Connect"));
            sprintf(buffer, _("Disconnect"));
            review_tree_vanpro(buffer, wifi);     
            wifi->my_wifi->wifi_cell[wifi->connect_index]->linkstate=LINK_DOWN;
            sprintf(buffer, (_("Wireless network selection")));
            set_title_label(buffer, wifi);
            link_count=0;
            show_connect_info(_("Not Connect."), wifi);
            wifi->RUNNING = FALSE;
            //doubleclick = TRUE ;
            return FALSE;
        }
        return TRUE;
        
    }else{
        wifi->linkconnect=TRUE;
        //send_atom_to_desktop("_NET_WIFI_CHANGE_STATUS", WIFI_STATUS_CONNECT);
        DBG("link connected,then we will require ip address.\n"); 
        sprintf(buffer, _("Connected"));
        review_tree_vanpro(buffer, wifi);
        memset(buffer, 0, 128);
        show_connect_info(_("Connected.\nAcquire IP address..."), wifi);
        wifi->RUNNING = TRUE;
        //require_ip_address(wifi);
        g_timeout_add(2000, (GSourceFunc)detect_network_interface_state,wifi);
        link_count=0;        
        return FALSE;
    }
}

gboolean do_connect(NetWorkWifi *wifi)
{
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }

    apply_configuration(wifi);
    g_timeout_add(2000,(GSourceFunc)detect_network_link_state,wifi);
    return FALSE;
}

static void connet(GtkWidget *widget, NetWorkWifi *wifi)
{
    GtkWidget *dlg;
    GtkWidget *entry;
    gchar *newval;
    char buffer[64];
    gint index = 0;

    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }

	//add shuai---in order to add key_mgmt and key index
    entry=g_object_get_data(G_OBJECT(wifi->mainwindow), "key_index");
    if (entry) 
    {   
        newval = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        index = atoi(newval);
        if ((index <= 4) && (index >= 1))
        {
            wifi->my_wifi->wifi_cell[wifi->connect_index]->key_index = index;
        }
        else
        {
            wifi->my_wifi->wifi_cell[wifi->connect_index]->key_index = 1;
        }
    }

    entry=g_object_get_data(G_OBJECT(wifi->mainwindow), "connect_key");
    if (entry) 
    {
        newval = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
        {
	        strcpy(wifi->my_wifi->wifi_cell[wifi->connect_index]->userkey, newval);
        }
    }
    wifi->my_wifi->wifi_cell[wifi->connect_index]->encmode=ENC_OPEN;

    dlg = g_object_get_data(G_OBJECT(wifi->mainwindow), "enc_dlg");
    if(dlg)
        gtk_widget_destroy(dlg);
    show_connect_info(_("Connecting network..."), wifi);
    while (gtk_events_pending())
    {
        gtk_main_iteration();
    }

    wifi->RUNNING = TRUE;
    g_timeout_add(100, (GSourceFunc)do_connect, wifi);
}

void cb_connect(NetWorkWifi *wifi) 
{        
    GtkWidget *dialog,*entry,*combobox,*ipdlg;
    gint response;
    gchar *newval;
    char buffer[64];
    gint msgret = 0;
    gint ipret = 0;
    
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }
    DBG("Choice to Connect index[%d],count[%d]\n", wifi->my_wifi->cell_index, wifi->my_wifi->cell_count);
    get_interface(wifi);
    if(wifi->my_wifi->interface_count<1){// No Device
        show_message_box(wifi->mainwindow, _("No Wireless Device"), TRUE);
        return;
    }
    
    if(wifi->my_wifi->cell_count<1){ //No Network
        show_message_box(wifi->mainwindow, _("No Network in Local Area"), TRUE);
        return;
    }    
    if(wifi->my_wifi->cell_index<0){ //No choice
        show_message_box(wifi->mainwindow, _("Select a Network First"), FALSE);
        return;
    }
    wifi->connect_index=wifi->my_wifi->cell_index;
    
    if(wifi->my_wifi->wifi_cell[wifi->connect_index]->linkstate==LINK_UP){
        printf("this cell is connect, will be link down!\n");
        g_timeout_add(100,(GSourceFunc)do_disconnect,wifi);
        wifi->my_wifi->wifi_cell[wifi->connect_index]->linkstate==LINK_DOWN;
        DBG("now is link down.\n");  
        return;
    }
    
    ipdlg = create_ipconfig_dlg(wifi);
    gtk_widget_show_all(ipdlg);
    msgret = gtk_dialog_run(GTK_DIALOG(ipdlg));
	if (msgret==RESPONSE_DHCP)
	{
        printf("dialog response dhcp\n");
        wifi->my_wifi->wifi_interface[0]->isstatic = FALSE;
        wifi->my_wifi->wifi_interface[0]->isdhcp = TRUE;
        wirte_interface_info(wifi->my_wifi->wifi_interface[0]);
        gtk_widget_destroy(ipdlg);
	}
    else if (msgret==RESPONSE_STATIC)
    {
        printf("dialog response static\n");
        gtk_widget_destroy(ipdlg);
        
        // set static ip dlg
        dialog=create_staticip_dlg(wifi);
        g_object_set_data(G_OBJECT(wifi->mainwindow), "ip_dlg", dialog);
        gtk_widget_show_all(dialog);
        ipret = gtk_dialog_run(GTK_DIALOG(dialog));
        if (ipret==GTK_RESPONSE_OK)
        {
            printf("dialog response ok\n");
            set_staticip(NULL, wifi);
        }
        else if (ipret==GTK_RESPONSE_REJECT)
        {
            printf("dialog response other\n");
            wifi->my_wifi->wifi_interface[0]->isstatic = FALSE;
            wifi->my_wifi->wifi_interface[0]->isdhcp = TRUE;
            wirte_interface_info(wifi->my_wifi->wifi_interface[0]);
            gtk_widget_destroy(dialog);
        }
    }
    
    if(strlen(wifi->my_wifi->wifi_cell[wifi->connect_index]->essid) < 1)
    {
        GtkWidget *parent = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(parent),
                                        GTK_DIALOG_DESTROY_WITH_PARENT,
                                        GTK_MESSAGE_QUESTION,
                                        GTK_BUTTONS_YES_NO,
                                        _("The essid is empty,Please input the Essid: "));

    	GtkWidget	*hbox=gtk_hbox_new(FALSE,0);
    	gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, TRUE, TRUE, 0);
    	GtkWidget *label=gtk_label_new(_("Essid:"));
    	gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
    	GtkWidget *entry=gtk_entry_new();
    	gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
        gtk_widget_show_all(dialog);
        
        if(gtk_dialog_run(GTK_DIALOG(dialog))==GTK_RESPONSE_YES)
        {
            const gchar* get_entry=gtk_entry_get_text(GTK_ENTRY(entry));
            strcpy(wifi->my_wifi->wifi_cell[wifi->connect_index]->essid, get_entry);
            printf("input essid = %s \n", wifi->my_wifi->wifi_cell[wifi->connect_index]->essid);
        }
        gtk_widget_destroy(dialog);
        
    }
    
    //
    gtk_rc_parse_string( "style \"font-default\"" \
                         "{font_name=\"Sans 12\"}" \
                         "class \"*\" style \"font-default\"" );
    if(g_str_equal(wifi->my_wifi->wifi_cell[wifi->connect_index]->key,"on"))
    {
        dialog=build_encrypted_dialog(wifi);
        g_object_set_data(G_OBJECT(wifi->mainwindow), "enc_dlg", dialog);
        gtk_widget_show_all(dialog);
        response=gtk_dialog_run(GTK_DIALOG(dialog));
        if(response==GTK_RESPONSE_OK)
        {
            connet(NULL, wifi);
        }
        else
        {
            if(dialog)
            gtk_widget_destroy(dialog);
        }

	//ask user whether add it to prefred network list

    }else{
        wifi->my_wifi->wifi_cell[wifi->connect_index]->encmode=ENC_OFF;
        show_connect_info(_("Connecting network..."), wifi);
        while (gtk_events_pending())
        {
            gtk_main_iteration();
        }
        wifi->RUNNING = TRUE;
        g_timeout_add(100, (GSourceFunc)do_connect, wifi);
    }  
}

static void wifi_connect(GtkMenuItem *menuitem, NetWorkWifi *wifi)
{
		printf("~~~%s\n" ,__FUNCTION__);
    GtkWidget *treeview;
    GtkTreeSelection *selection;
    GtkTreePath *path;
    gint *sel_index;
    GtkTreeModel *model;
    GtkTreeIter iter;

    // check param
    if (NULL == wifi)
    {
        printf("input param invalid in %s\n", __func__);
        return;
    }

    treeview = g_object_get_data(G_OBJECT(wifi->mainwindow), "view-press");
    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
    model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
    if (!gtk_tree_selection_get_selected(selection, (GtkTreeModel **)&model, &iter))
    {
        DBG("there is no selected signal, can't connect!\n");
        return;
    }
    
    gchar *type = NULL;
    gtk_tree_model_get(model, &iter, COL_KEY, &type, -1);
    if (wifi->type != NULL)
    {
        g_free(wifi->type);
    }
    wifi->type = g_strdup_printf("%s", type);
    printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@type = %s\n" ,type);
    
    //gtk_tree_view_get_path_at_pos(tree_view, event->x, event->y, &tree_path, NULL, NULL, NULL);
    path = gtk_tree_model_get_path(model, &iter);
    if (path) 
    {
        sel_index = gtk_tree_path_get_indices(path);
        if (sel_index) 
        {
            wifi->my_wifi->cell_index = *sel_index;
        }
        if (gtk_tree_selection_path_is_selected(selection, path)) 
        {
            gtk_tree_selection_unselect_all(selection);
            gtk_tree_selection_select_path(selection, path);            
        }
        gtk_tree_path_free(path);
    }
    
    g_return_if_fail(wifi->my_wifi->cell_index>-1);
    g_return_if_fail(wifi->my_wifi->wifi_cell[wifi->my_wifi->cell_index] != NULL);   
    
    DBG("connect_index = %d, my_wifi->cell_index=[%d]\n", 
        wifi->connect_index, wifi->my_wifi->cell_index);
    if (wifi->my_wifi->wifi_cell[wifi->my_wifi->cell_index]->linkstate != LINK_UP)
    {
        cb_connect(wifi);
    }
    return;
}

//gboolean cb_view_sel(GtkWidget *widget, GdkEventButton *event, NetWorkWifi *wifi)
void cb_view_sel(GtkTreeView *tree_view, GtkTreePath *path,
                    GtkTreeViewColumn *column,
                    NetWorkWifi *wifi)
{
    GtkTreeSelection *tree_sel;
    GtkTreePath *tree_path;
    GtkWidget *button;
    gint *sel_index;
    GdkEventKey *key;
    GtkTreeModel *model;
    GtkTreeIter iter;

    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }
    wifi->RUNNING = TRUE;
    DBG("cb_view_sel start!!!\n");
    //printf("cell cnt = %d, interface cnt = %d\n", wifi->my_wifi->cell_count, wifi->my_wifi->interface_count);
    g_return_if_fail(wifi->my_wifi->cell_count>0
                    && wifi->my_wifi->interface_count>0);
    
    DBG("Cell count %d index %d interface count %d\n",
        wifi->my_wifi->cell_count, wifi->my_wifi->cell_index, wifi->my_wifi->interface_count);
#if 0
    tree_sel = gtk_tree_view_get_selection(tree_view);
    model = gtk_tree_view_get_model(tree_view);
    if (!gtk_tree_selection_get_selected(tree_sel, (GtkTreeModel **)&model, &iter))
    {
        DBG("there is no selected signal, can't connect!\n");
        return;
    }
    
    gchar *type = NULL;
    gtk_tree_model_get(model, &iter, COL_KEY, &type, -1);
    if (wifi->type != NULL)
    {
        g_free(wifi->type);
    }
    wifi->type = g_strdup_printf("%s", type);
    
    //gtk_tree_view_get_path_at_pos(tree_view, event->x, event->y, &tree_path, NULL, NULL, NULL);
    tree_path = gtk_tree_model_get_path(model, &iter);
    if (tree_path) 
    {
        sel_index = gtk_tree_path_get_indices(tree_path);
        if (sel_index) 
        {
            wifi->my_wifi->cell_index = *sel_index;
        }
        if (gtk_tree_selection_path_is_selected(tree_sel, tree_path)) 
        {
            gtk_tree_selection_unselect_all(tree_sel);
            gtk_tree_selection_select_path(tree_sel, tree_path);            
        }
        gtk_tree_path_free(tree_path);
    }
    
    g_return_if_fail(wifi->my_wifi->cell_index>-1);
    g_return_if_fail(wifi->my_wifi->wifi_cell[wifi->my_wifi->cell_index] != NULL);   
    
    DBG("connect_index = %d, my_wifi->cell_index=[%d]\n", 
        wifi->connect_index, wifi->my_wifi->cell_index);
    if (wifi->my_wifi->wifi_cell[wifi->my_wifi->cell_index]->linkstate != LINK_UP)
    {
        cb_connect(wifi);
    }
#else
    wifi_connect(NULL, wifi);
#endif
    return;
}

static void arrange_tree_view(GtkTreeView *treeview)
{
    GtkCellRenderer *render;
    GtkTreeViewColumn *column;

    // id invisible
    render = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes(_("ESSID"), render, "text", COL_ESSID, NULL);
    gtk_tree_view_append_column(treeview, column);

    // playURL
    render = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes (_("Mode"), render, "text", COL_MODE, NULL); 
    gtk_tree_view_append_column (treeview, column);
    gtk_tree_view_column_set_visible(column, FALSE);

    // icon
    render = gtk_cell_renderer_text_new();
#ifdef WEP_CHECK
    column = gtk_tree_view_column_new_with_attributes (_("Type"), render, "text", COL_KEY, NULL);
#else
    column = gtk_tree_view_column_new_with_attributes (_("KEY"), render, "text", COL_KEY, NULL);
#endif
    gtk_tree_view_append_column(treeview, column);

    // title
    render = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes (_("Signal"), render, "text", COL_QUALITY, NULL);
    gtk_tree_view_append_column(treeview, column);  

    // type(video or list)
    render = gtk_cell_renderer_text_new();
    column = gtk_tree_view_column_new_with_attributes (_("State"), render, "text", COL_STATE, NULL); 
    gtk_tree_view_append_column(treeview, column);
    //gtk_tree_view_column_set_visible(column, FALSE);
}

void model_data_new(GtkTreeModel* store,
					const gchar* ESSID,const gchar* Mode,const gchar* Key,
					const gchar* Quality,const gchar *State) 
{
    GtkTreeIter iter;

    gtk_list_store_append(GTK_LIST_STORE(store), &iter);
    gtk_list_store_set(GTK_LIST_STORE(store), &iter,
                        COL_ESSID, ESSID,
						COL_MODE, Mode,
                        COL_KEY, Key,
                        COL_QUALITY, Quality,
                        COL_STATE, State,
                        -1);
}

GtkTreeModel* create_model() 
{
    GtkListStore  *store;
    
    store = gtk_list_store_new(n_cols, G_TYPE_STRING, G_TYPE_STRING,
                                G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
    return GTK_TREE_MODEL(store);
}

GtkWidget *create_list(NetWorkWifi *wifi) 
{
    GtkWidget *vbox;
    GtkWidget *label;
    GtkWidget *table;    
    GtkWidget *button;
    GtkWidget *scrolled_window;
    GtkWidget *list_scrolled_window;

    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }

    vbox = gtk_vbox_new(FALSE, 10);
    //label = gtk_label_new(_("Wireless network selection"));
    label = gtk_label_new(_("Press up or down to select a signal, then press OK to connect, or Esc to quit."));
    
    gtk_widget_set_name(label, "title-label");
    gtk_object_set_data(GTK_OBJECT(wifi->mainwindow), "title-label", label);
    gtk_misc_set_alignment(GTK_MISC(label), 0.5, 0.5);
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 8);

    GtkWidget* view;
    view = gtk_tree_view_new();
    gtk_widget_set_name(view, "view-press");
    gtk_object_set_data(GTK_OBJECT(wifi->mainwindow), "view-press", view);

    // arrange view columns
    arrange_tree_view(GTK_TREE_VIEW(view));
    g_signal_connect(G_OBJECT(view), "row-activated", G_CALLBACK(cb_view_sel), wifi);

    // set model
    GtkTreeModel* store = create_model();
    g_object_set_data(G_OBJECT(wifi->mainwindow), "cell-list", store);
    gtk_tree_view_set_model(GTK_TREE_VIEW(view), store);
    g_object_unref(store);    
   
   
    list_scrolled_window = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(list_scrolled_window),
                                   GTK_POLICY_ALWAYS,
                                   GTK_POLICY_AUTOMATIC);//GTK_POLICY_AUTOMATIC
   
    //gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(list_scrolled_window), view);
    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(list_scrolled_window),
                                         GTK_SHADOW_IN);
    gtk_container_add(GTK_CONTAINER(list_scrolled_window), view);
    gtk_box_pack_start(GTK_BOX(vbox), list_scrolled_window, TRUE, TRUE, 0);

    wifi->my_wifi->wifi_view = view;
    gtk_widget_show_all(vbox);  
    return vbox;
}

static void set_title_label(gchar *text, NetWorkWifi *wifi)
{
    GtkWidget *label;

    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }

    label=g_object_get_data(G_OBJECT(wifi->mainwindow), "title-label");
    if(label){
        gtk_label_set_text(GTK_LABEL(label),text);
    }
}

static void show_message_box(GtkWidget *parent, gchar *message, gboolean quit)
{
    GtkWidget *dialog;
    dialog = gtk_message_dialog_new(GTK_WINDOW(parent),
                                    GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_INFO,
                                    GTK_BUTTONS_CLOSE,
                                    message);
    gtk_widget_show_all(dialog);
    gtk_dialog_run(GTK_DIALOG(dialog));
    if (quit)
    {
        gtk_main_quit();
    }
    gtk_widget_destroy(dialog);
}

//void get_cell(WIFI_INTERFACE *iface)
void get_cell(NetWorkWifi *wifi)
{
	FILE *pipe;
	gchar *cmd;
	gchar *search_prefix;
	gchar **tmpline;
	gchar tmpessid[64];
	gchar buffer[512];
	gchar tmpbuffer[128];
	gchar ap_address[32];
	gchar *value;
    gchar *idstart = NULL;
    gchar *idend = NULL;
	gint tmpi;
#ifdef WEP_CHECK
    gchar *keywords[9]={"Address:","ESSID:","Mode:","Frequency:","Quality=","Quality:","Encryption key:","Driver:", "Encryption type:"};
    unsigned char keywords_used[9]; 
#else
	gchar *keywords[7]={"Address:","ESSID:","Mode:","Frequency:","Quality:","Encryption key:","Bit Rates:"};
	unsigned char keywords_used[7]; 
#endif
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }

	DBG("get cell start...\n");
    //printf("cell cnt = %d, interface cnt = %d\n", wifi->my_wifi->cell_count, wifi->my_wifi->interface_count);
    WIFI_INTERFACE *iface = wifi->my_wifi->wifi_interface[0];
	cmd=g_strdup_printf("/sbin/ifconfig %s up",iface->name);
	system(cmd);
    g_free(cmd);
	cmd=g_strdup_printf("iwlist %s scan",iface->name);	
	search_prefix=g_strdup("          Cell");
	pipe = popen (cmd, "r");
	if(!pipe){perror("pipe:");return;}
	gint k=0,i;
	gint ad=0;
	wifi->my_wifi->cell_count=0;
	memset(buffer,0,512);

    gint num = sizeof(keywords_used);
	if(pipe){
		while(fgets(buffer,512,pipe))
		{
			if(strstr(buffer,"Cell"))
			{
				DBG("FIND CELL[%d]\n",k);
				memset(keywords_used,0,num);				
				wifi->my_wifi->wifi_cell[k]=(WIFI_CELL *)g_malloc0(sizeof(WIFI_CELL));  
				memset(wifi->my_wifi->wifi_cell[k],0,sizeof(WIFI_CELL));
				strcpy(wifi->my_wifi->wifi_cell[k]->state,_("Disconnect"));
				wifi->my_wifi->wifi_cell[k]->linkstate=LINK_DOWN;    
				
				do{
					for(i=0;i<num;i++)
					{
						if(keywords_used[i]==1)
							continue;
                        if(i==1)
                            value=get_essid_value(buffer,keywords[1],0);
                        else
                            value=get_string_value(buffer,keywords[i],0);

                        if(value)
						{
							keywords_used[i]=1;
							break;
						}
					}
					if(i<num)
					{
    					switch (i){
    						case 0:
    							strcpy(wifi->my_wifi->wifi_cell[k]->address,value);
    							g_free(value);
    							DBG("Address:%s\n",wifi->my_wifi->wifi_cell[k]->address);
    							break;
    						case 1:	
                            #if 0
    							if(value[0]=='"')
    								value[0]=' ';
    							if(value[strlen(value)-1]=='"')                                  
    								value[strlen(value)-1]=' ';                                
    							strcpy(wifi->my_wifi->wifi_cell[k]->essid,g_strstrip(value));
    							g_free(value);
                            #else
                                idstart = strstr(value, "\"");
                                if (idstart != NULL)
                                {
                                    idend = strstr(idstart+1, "\"");
                                    strncpy(wifi->my_wifi->wifi_cell[k]->essid, idstart+1, idend-idstart-1);
                                }
                                else
                                {
                                    strcpy(wifi->my_wifi->wifi_cell[k]->essid, value);
                                }
                            #endif
    							DBG("ESSID:%s\n",wifi->my_wifi->wifi_cell[k]->essid);
    							break;
    						case 2:
    							strcpy(wifi->my_wifi->wifi_cell[k]->mode,value);
    							g_free(value);
    							DBG("Mode:%s\n",wifi->my_wifi->wifi_cell[k]->mode);
    							break;
    						case 3:
    							strcpy(wifi->my_wifi->wifi_cell[k]->channel,value);
    							g_free(value);
    							DBG("Frequency:%s\n",wifi->my_wifi->wifi_cell[k]->channel);
    							break;
    						case 4:	
                            case 5:
    							strcpy(wifi->my_wifi->wifi_cell[k]->quality,value);
    							g_free(value);
    							DBG("quality:%s\n",wifi->my_wifi->wifi_cell[k]->quality);
    							break;
    						case 6:
    							strcpy(wifi->my_wifi->wifi_cell[k]->key, value);			
    							if(g_str_equal(value, "on"))
    								wifi->my_wifi->wifi_cell[k]->encmode=ENC_OPEN;
    							else if(g_str_equal(value, "off"))
    								wifi->my_wifi->wifi_cell[k]->encmode=ENC_OFF;
    							else
    								wifi->my_wifi->wifi_cell[k]->encmode=ENC_RESTRICTED;
    							g_free(value);
    							DBG("key:%d\n",wifi->my_wifi->wifi_cell[k]->encmode);
    							break;
    						case 7:
                            #ifdef WEP_CHECK
                                strcpy(wifi->my_wifi->wifi_cell[k]->device_type,value);
                                g_free(value);
                                DBG("device type:%s\n",wifi->my_wifi->wifi_cell[k]->device_type);
                                break;
                            case 8:
                                strcpy(wifi->my_wifi->wifi_cell[k]->encryption_type,value);
                                g_free(value);
                                DBG("encryption type:%s\n",wifi->my_wifi->wifi_cell[k]->encryption_type);
                            #endif
    							goto next_cell;
    							break;
    						default:
    							break;
    					}
					}					
				}while(fgets(buffer,512,pipe));
				
next_cell:	
				if(CELL_COUNT==wifi->my_wifi->cell_count)
					break;
				k++;
				wifi->my_wifi->cell_count++;
			}
		}
	}
	pclose(pipe);
    DBG("get cell end, cnt = %d!!!\n", wifi->my_wifi->cell_count);
    //printf("cell , interface cnt = %d\n", , wifi->my_wifi->interface_count);
}

static void get_ap_mac(NetWorkWifi *wifi)
{
    int inet_sock;
    struct iwreq iwr;
    
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }

	memset(&iwr, 0, sizeof(iwr));
    memset(wifi->Link_AP_MAC,0,48);  
    inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
    strcpy(iwr.ifr_name, wifi->my_wifi->wifi_interface[0]->name);
    memset(wifi->Link_AP_MAC, 0, 48);
    if(ioctl(inet_sock, SIOCGIWAP, &iwr) >= 0) 
    {                  
        sprintf(wifi->Link_AP_MAC, "%02X:%02X:%02X:%02X:%02X:%02X",
        (unsigned char)iwr.u.ap_addr.sa_data[0],
        (unsigned char)iwr.u.ap_addr.sa_data[1],
        (unsigned char)iwr.u.ap_addr.sa_data[2],
        (unsigned char)iwr.u.ap_addr.sa_data[3],
        (unsigned char)iwr.u.ap_addr.sa_data[4],
        (unsigned char)iwr.u.ap_addr.sa_data[5]);  
        DBG("Detect Linked Acess Pointer:%s\n", wifi->Link_AP_MAC);
    }
}

static void get_interface_statistics_data(WIFI_INTERFACE *iface)
{
    int inet_sock;
    struct ifreq ifr;
    
    inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
    strcpy(ifr.ifr_name, iface->name);
    if (ioctl(inet_sock, SIOCGIFADDR, &ifr) < 0)
    {
        strcpy(iface->address, "");
        perror("ioctl");
    }
    else{
        strcpy(iface->address, inet_ntoa(((struct sockaddr_in*)&(ifr.ifr_addr))->sin_addr));
        DBG("address:%s\n", iface->address);
    }
    close(inet_sock);
}

static void review_tree_vanpro(char *data, NetWorkWifi *wifi)
{
    int i;
    GtkWidget *cell_list;
    
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }
    
    cell_list=gtk_object_get_data(GTK_OBJECT(wifi->mainwindow), "cell-list");
    if(cell_list){
        gtk_list_store_clear((GtkListStore *)cell_list);
    }else
        return;
//    pixbuf=gdk_pixbuf_new_from_file("/share/pixmaps/pccard-network.png",NULL);
    for(i=0;i<wifi->my_wifi->cell_count;i++){
        strcpy(wifi->my_wifi->wifi_cell[i]->state,_("Disconnect"));
        wifi->my_wifi->wifi_cell[i]->linkstate=LINK_DOWN;
        if(i==wifi->connect_index){//if wifi_cell[i] has the connect_index hw address
            strcpy(wifi->my_wifi->wifi_cell[wifi->connect_index]->state,data); 
            wifi->my_wifi->wifi_cell[wifi->connect_index]->linkstate=LINK_UP;
        }
        model_data_new((GtkTreeModel* )cell_list,
        wifi->my_wifi->wifi_cell[i]->essid,
		wifi->my_wifi->wifi_cell[i]->mode,
	#ifdef WEP_CHECK
        wifi->my_wifi->wifi_cell[i]->encryption_type,
    #else
        wifi->my_wifi->wifi_cell[i]->key,
    #endif
        wifi->my_wifi->wifi_cell[i]->quality,
        wifi->my_wifi->wifi_cell[i]->state        
        );
    }

    GtkWidget *treeview;
    GtkTreeSelection *selection;
    treeview = g_object_get_data(G_OBJECT(wifi->mainwindow), "view-press");
    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
    GtkTreePath *path = gtk_tree_path_new_from_string("0");
    gtk_tree_selection_select_path(selection, path);
}

gboolean cb_refresh(NetWorkWifi *wifi) 
{
    GtkWidget *dialog;
    GtkWidget *button;
    gint i;
    char buffer[128];
    
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }

    //get_cell(my_wifi->wifi_interface[0]);   
    get_cell(wifi);     
    button = g_object_get_data(G_OBJECT(wifi->mainwindow), "view-press");
    if (button) {
        gtk_widget_set_sensitive(button, TRUE);
    }   
    wifi->my_wifi->cell_index=-1;
    if(wifi->my_wifi->cell_count<1){
        //sprintf(buffer, _("No Network"));
        //set_title_label(buffer, wifi);
        set_title_label(_("No Network"), wifi);
        show_message_box(wifi->mainwindow, _("No Network"), FALSE);
        sprintf(buffer, _("Wireless network selection"));
        set_title_label(buffer, wifi); 
        return FALSE;
    }    
    
    get_ap_mac(wifi);
    get_interface_statistics_data(wifi->my_wifi->wifi_interface[0]);
    wifi->connect_index=-1;
    memset(buffer,0,128);
    for(i=0;i<wifi->my_wifi->cell_count;i++){
        wifi->my_wifi->wifi_cell[i]->linkstate=LINK_DOWN;
        if(g_str_equal(g_strstrip(wifi->Link_AP_MAC), wifi->my_wifi->wifi_cell[i]->address)){
            DBG("Match_AP_MAC:%s,address:%s\n", wifi->Link_AP_MAC, wifi->my_wifi->wifi_cell[i]->address);
            wifi->my_wifi->wifi_cell[i]->linkstate=LINK_UP;            
            wifi->connect_index=i;
            
            if(g_str_equal(wifi->my_wifi->wifi_interface[0]->address, "")){
                //sprintf(buffer, _("Connected\nIP address invalid"));
                sprintf(buffer, _("Invalid IP"));
                DBG("connect_index[%d] BUFFER:%s\n",wifi->connect_index,buffer);
            }else{
                //sprintf(buffer, "%s:%s", _("Connected\nIP Address"), wifi->my_wifi->wifi_interface[0]->address);
                sprintf(buffer, _("Connected"));
                DBG("connect_index[%d] BUFFER:%s, IP: %s\n",wifi->connect_index, buffer, wifi->my_wifi->wifi_interface[0]->address);
            }
            DBG("matched connect info:state [%d],essid:[%s]",
            wifi->my_wifi->wifi_cell[wifi->connect_index]->linkstate,
            wifi->my_wifi->wifi_cell[wifi->connect_index]->essid);            
        }
    }
    if(wifi->connect_index==-1)
        sprintf(buffer, _("Disconnect"));
    review_tree_vanpro(buffer, wifi);
    sprintf(buffer, _("Wireless network selection"));
    set_title_label(buffer, wifi);
//    pixbuf=gdk_pixbuf_new_from_file("/share/pixmaps/pccard-network.png",NULL);
    return FALSE;
}

gboolean cb_refresh_network(GtkWidget *cb_widget, NetWorkWifi *wifi)
{
    GtkWidget *cell_list;
    GtkWidget *button,*dialog;
    char buffer[128];
    DBG("Searching...\n");
	
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }

    sprintf(buffer, _("Searching..."));
    set_title_label(buffer, wifi);
    get_interface(wifi);
    
    printf("my_wifi->interface_count = %d\n", wifi->my_wifi->interface_count);
    if (wifi->my_wifi->interface_count<1){
        sprintf(buffer, _("No Wireless Device"));
        set_title_label(buffer, wifi);
        show_message_box(wifi->mainwindow, _("No Wireless Device"), TRUE);
        DBG("No interface\n");
        return FALSE;
    }
    
	if(wifi->my_wifi->wifi_interface[0]->wifi_group_enable)
	{
		GtkWidget *widget;
		widget=gtk_object_get_data(GTK_OBJECT(wifi->mainwindow), "view-press");
		gtk_widget_set_sensitive(widget, FALSE);
		widget=gtk_object_get_data(GTK_OBJECT(wifi->mainwindow), "title-label");
		gtk_label_set_label(GTK_LABEL(widget),_("Ad-Hoc Host Mode"));
		return FALSE;
	}
	
    //let widget unused
    cell_list=gtk_object_get_data(GTK_OBJECT(wifi->mainwindow), "cell-list");
    if(cell_list){
        gtk_list_store_clear((GtkListStore *)cell_list);
    }
    
    button = g_object_get_data(G_OBJECT(wifi->mainwindow), "view-press");
    if (button) {
        gtk_widget_set_sensitive(button, FALSE);
    }    
	
    g_timeout_add(100, (GSourceFunc)cb_refresh, wifi);
    return FALSE;
}

gboolean cb_check_wifi(NetWorkWifi *wifi)
{
	GtkWidget *button;
	GtkWidget *proc_win;
	
    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }
    
    proc_win = g_object_get_data(G_OBJECT(wifi->mainwindow), "window-proc-win");
	printf("proc_win = %p\n", proc_win);
    
	if (proc_win)
    {
		gtk_widget_hide(proc_win);
		gtk_widget_destroy(GTK_WIDGET(proc_win));
	}

	get_interface(wifi);
	if (wifi->my_wifi->interface_count < 1)
    {
		show_message_box(NULL, _("Can not found any WIFI device. Please confirm that you have a WIFI device (either built-in or USB WIFI)."), TRUE);
	}
	else
    {
        cb_refresh_network(NULL, wifi);
	}
	
	return FALSE;
}

void cb_active_device(NetWorkWifi *wifi)
{
	GtkLabel *text;
	GtkWidget *vbox;

    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }
	if (access("/etc/build_in_wifi", F_OK) == 0)
	{
		get_interface(wifi);
		if (wifi->my_wifi->interface_count > 0)
        {
			cb_refresh_network(NULL, wifi);
			return;
		}
	}
	else
	{
		cb_refresh_network(NULL, wifi);
		return;
	}
	
	//active build-in wifi 
	GtkWidget *proc_win = gtk_window_new(GTK_WINDOW_POPUP);
	gtk_widget_set_name(proc_win, "window-proc-win");
	g_object_set_data(G_OBJECT(wifi->mainwindow), "window-proc-win", proc_win);

	//active inbuilt wifi
	char cmd[128];
#ifdef WEP_CHECK
    sprintf(cmd, "echo 1 > /proc/iwifi");
#else
	sprintf(cmd, "echo 1 > /proc/jz/iwifi");
#endif
	system(cmd);

	vbox = gtk_vbox_new(FALSE, 0);
	text = GTK_LABEL(gtk_label_new(_("Checking, please wait...")));
	gtk_label_set_justify(text, GTK_JUSTIFY_CENTER);
	gtk_box_pack_start(GTK_BOX(vbox),GTK_WIDGET(text),FALSE,FALSE,0);
	gtk_container_add(GTK_CONTAINER(proc_win), vbox);
	gtk_window_set_position(GTK_WINDOW (proc_win), GTK_WIN_POS_CENTER);
	
	g_timeout_add(6050, (GSourceFunc)cb_check_wifi, wifi);
	gtk_widget_show_all(proc_win);
	while(gtk_events_pending())
	{
		gtk_main_iteration();
    }
	sleep(6);
}

static gint esc_key_press(GtkWidget *widget, GdkEventButton *event, NetWorkWifi *wifi)
{
    GdkEventKey *key;

    // check param
    if (NULL == wifi)
    {
        printf("input param invalid in %s\n", __func__);
        return FALSE;
    }
    
    key = (GdkEventKey *)event;
    //fprintf(stderr, "key value: %d\n", key->keyval);
    switch (key->keyval)
    {
    case GDK_Escape:
        gtk_main_quit();
        return TRUE;
        
    case GDK_Z:
    case GDK_z: 
        if ((key->state&GDK_CONTROL_MASK) && (key->state&GDK_MOD1_MASK))
        {
            wifi_show_menu_dialog(wifi);
            return TRUE;
        }
        break;
        
    default:
        break;
    }

    return FALSE;
}

static gint key_press_event(GtkWidget *widget, GdkEventButton *event, NetWorkWifi *wifi)
{
    if (wifi->RUNNING)
    {
        return TRUE;
    }

    gtk_widget_hide(widget);
    return FALSE;
}

static void show_connect_info(gchar *info, NetWorkWifi *wifi)
{
    GtkWidget *label;
    GtkWidget *vbox;

    // check param
    if (info == NULL || wifi == NULL)
    {
        DBG("input param invalid in %s\n", __func__);
        return;
    }
#if 0
    label = g_object_get_data(G_OBJECT(wifi->connect_window), "label");
    gtk_misc_set_alignment(GTK_MISC(label), 0.5, 0.5);
    gtk_label_set_text(GTK_LABEL(label), info);
#else
    vbox = g_object_get_data(G_OBJECT(wifi->connect_window), "label_vbox");
    label = g_object_get_data(G_OBJECT(wifi->connect_window), "label");
    gtk_container_remove(GTK_CONTAINER(vbox), label);
    label = gtk_label_new(info);
    g_object_set_data(G_OBJECT(wifi->connect_window), "label", label);
    //gtk_box_pack_start(GTK_BOX(vbox),label,FALSE,FALSE,0);
    gtk_container_add(GTK_CONTAINER(vbox), label);
#endif
    gtk_widget_show_all(wifi->connect_window);
}

#if 1 //wkw's code 0727
static GtkWidget* wifi_creat_menu(NetWorkWifi *wifi)
{
    GtkWidget *item, *image;

    // check param
    if (NULL == wifi)
    {
        printf("input param invalid in %s\n", __func__);
        return NULL;
    }

#if 1 //set menu font
    gtk_rc_parse_string( "style \"menu-font\"{font_name=\"Sans 12\"}" \
                        "class \"GtkMenu\" style \"menu-font\"" \
                        "class \"GtkMenuItem\" style \"menu-font\"" \
                        "class \"GtkImageMenuItem\" style \"menu-font\"" \
                        "widget_class \"*.GtkMenuItem.*\" style \"menu-font\"" \
                        "widget_class \"*.GtkImageMenuItem.*\" style \"menu-font\"");
#endif
    GtkWidget *menu = gtk_menu_new();

    image = gtk_image_new_from_stock(GTK_STOCK_CONNECT,GTK_ICON_SIZE_DND);
    item = gtk_image_menu_item_new_with_label(_("Connect"));
    gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),image);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
    g_signal_connect(G_OBJECT(item), "activate",
                    G_CALLBACK(wifi_connect), wifi);
    g_object_set_data(G_OBJECT(wifi->mainwindow),"wifi-menu-connect",(gpointer)item);

    image=gtk_image_new_from_stock(GTK_STOCK_DISCONNECT,GTK_ICON_SIZE_DND);
    item = gtk_image_menu_item_new_with_label(_("Disconnect"));
    gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),image);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
    g_signal_connect(G_OBJECT(item), "activate",
                    G_CALLBACK(wifi_disconnect), wifi);
    g_object_set_data(G_OBJECT(wifi->mainwindow),"wifi-menu-disconnect",(gpointer)item);

    image=gtk_image_new_from_stock(GTK_STOCK_REFRESH,GTK_ICON_SIZE_DND);
    item = gtk_image_menu_item_new_with_label(_("Refresh"));
    gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),image);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
    g_signal_connect(G_OBJECT(item), "activate",
                    G_CALLBACK(cb_refresh_network), wifi);
    g_object_set_data(G_OBJECT(wifi->mainwindow),"wifi-menu-refresh",(gpointer)item);

    image=gtk_image_new_from_stock(GTK_STOCK_INFO,GTK_ICON_SIZE_DND);
    item = gtk_image_menu_item_new_with_label(_("Info"));
    gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(item),image);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
    g_signal_connect(G_OBJECT(item), "activate", G_CALLBACK(show_info), wifi);
    g_object_set_data(G_OBJECT(wifi->mainwindow),"wifi-menu-info",(gpointer)item);

    gtk_widget_show_all(menu);
    return menu;
}

static void wifi_menu_position(GtkMenu *menu,
                                gint *x,
                                gint *y,
                                gboolean *push_in,
                                gpointer user_data)
{
    GtkWidget *widget=user_data;
    GtkRequisition requisition;
    int w1,h1,w2,h2;

    w1=widget->allocation.width;
    h1=widget->allocation.height;

    gtk_widget_size_request (GTK_WIDGET(menu),&requisition);
    w2=requisition.width;
    h2=requisition.height;

    *x=(w1-w2)/2;
    *y=(h1-h2)/2;
    printf("w1 = %d, w2 = %d, h1 = %d, h2 = %d, x = %d, y = %d\n", w1, w2, h1, h2, *x, *y);
}

static gboolean
wifi_menu_key_press_event_cb (GtkWidget* menu,
                                GdkEventKey* event,
                                NetWorkWifi *wifi)
{
    if ((event->keyval == 122) && 
        (event->state&GDK_CONTROL_MASK)&&(event->state&GDK_MOD1_MASK))
    {
        gtk_menu_popdown(GTK_MENU(menu));
        return TRUE;
    }
    return FALSE;
}

static void
wifi_show_menu_dialog(NetWorkWifi *wifi)
{
    // check param
    if (NULL == wifi)
    {
        printf("input param invalid in %s\n", __func__);
        return;
    }
    
    GtkWidget* 
    menu=(GtkWidget*)g_object_get_data(G_OBJECT(wifi->mainwindow),"wifi-menu");
    if(!menu)
    {
        menu=wifi_creat_menu(wifi);
        g_object_set_data(G_OBJECT(wifi->mainwindow),"wifi-menu",(gpointer)menu);
        g_signal_connect (G_OBJECT(menu), "key-press-event",
                        G_CALLBACK (wifi_menu_key_press_event_cb), wifi);
    }

    GtkWidget* connect,*disconnect;
    connect=g_object_get_data(G_OBJECT(wifi->mainwindow),"wifi-menu-connect");
    disconnect=g_object_get_data(G_OBJECT(wifi->mainwindow),"wifi-menu-disconnect");

    g_return_if_fail(wifi->my_wifi->cell_count>0
                    && wifi->my_wifi->interface_count>0);
    
    GtkWidget *treeview;
    GtkTreeSelection *selection;
    GtkTreePath *path;
    gint *sel_index;
    GtkTreeModel *model;
    GtkTreeIter iter;
    treeview = g_object_get_data(G_OBJECT(wifi->mainwindow), "view-press");
    selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
    model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
    if (!gtk_tree_selection_get_selected(selection, (GtkTreeModel **)&model, &iter))
    {
        DBG("there is no selected signal, can't connect!\n");
        return;
    }
    
    //gtk_tree_view_get_path_at_pos(tree_view, event->x, event->y, &tree_path, NULL, NULL, NULL);
    path = gtk_tree_model_get_path(model, &iter);
    if (path) 
    {
        sel_index = gtk_tree_path_get_indices(path);
        if (sel_index) 
        {
            wifi->my_wifi->cell_index = *sel_index;
        }
        if (gtk_tree_selection_path_is_selected(selection, path)) 
        {
            gtk_tree_selection_unselect_all(selection);
            gtk_tree_selection_select_path(selection, path);            
        }
        gtk_tree_path_free(path);
    }
    
    g_return_if_fail(wifi->my_wifi->cell_index>-1);
    g_return_if_fail(wifi->my_wifi->wifi_cell[wifi->my_wifi->cell_index] != NULL);   
    
    DBG("connect_index = %d, my_wifi->cell_index=[%d]\n", 
        wifi->connect_index, wifi->my_wifi->cell_index);
    if (wifi->my_wifi->wifi_cell[wifi->my_wifi->cell_index]->linkstate != LINK_UP)
    {
        gtk_widget_set_sensitive(connect, TRUE);
        gtk_widget_set_sensitive(disconnect, FALSE);
    }
    else
    {
        gtk_widget_set_sensitive(connect, FALSE);
        gtk_widget_set_sensitive(disconnect, TRUE);
    }

    gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
                    NULL, wifi, 0, 0);//wifi_menu_position
}
#endif //end wkw code

/****************** extern Func definition area ******************/
gint show_wifi(NetWorkWifi *wifi)
{
    GtkWidget *vbox;
    GtkWidget *list;
    GtkWidget *mainwindow;

    // check param
    if (NULL == wifi)
    {
        DBG("input param invalid in %s!!!\n", __FUNCTION__);
        return;
    }
    
    system("echo 1 > /proc/iwifi && sleep 1");
    if (!my_wifi_init(wifi))
    {
        DBG("my_wifi_init error!!!\r\n");
        return -1;
    }
    
    gtk_rc_parse_string( "style \"font-default\"" \
                         "{font_name=\"Sans 14\"}" \
                         "class \"*\" style \"font-default\"" );

    mainwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    wifi->mainwindow = mainwindow;
    gtk_window_fullscreen(GTK_WINDOW(wifi->mainwindow));
    gtk_container_set_border_width(GTK_CONTAINER(mainwindow), 20);
    g_signal_connect(G_OBJECT(mainwindow), "destroy",
                    G_CALLBACK(gtk_main_quit), NULL);
    gtk_signal_connect(GTK_OBJECT(mainwindow),"key-press-event",GTK_SIGNAL_FUNC(esc_key_press),wifi);

    vbox = gtk_vbox_new(FALSE, 0);
    gtk_container_add(GTK_CONTAINER(mainwindow), vbox);
    
    list = create_list(wifi);
    gtk_box_pack_start(GTK_BOX(vbox), list, TRUE, TRUE, 0);
    gtk_widget_show_all(mainwindow);

    // connecting info window
    wifi->connect_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);//GTK_WINDOW_POPUP
    gtk_window_set_type_hint(GTK_WINDOW(wifi->connect_window), GDK_WINDOW_TYPE_HINT_DIALOG);
    gtk_window_set_decorated(GTK_WINDOW(wifi->connect_window), FALSE);
    gtk_widget_add_events(GTK_WIDGET(wifi->connect_window), GDK_KEY_PRESS_MASK);
    g_signal_connect(wifi->connect_window, "key-press-event", G_CALLBACK(key_press_event), wifi);
    
    vbox = gtk_vbox_new(FALSE, 0);
    g_object_set_data(G_OBJECT(wifi->connect_window), "label_vbox", vbox);
    GtkWidget *label = gtk_label_new("");
    g_object_set_data(G_OBJECT(wifi->connect_window), "label", label);
    //gtk_box_pack_start(GTK_BOX(vbox),label,FALSE,FALSE,0);
    gtk_container_add(GTK_CONTAINER(vbox), label);
    gtk_container_add(GTK_CONTAINER(wifi->connect_window), vbox);
    gtk_window_set_position(GTK_WINDOW (wifi->connect_window), GTK_WIN_POS_CENTER);

    GtkWidget *view = gtk_object_get_data(GTK_OBJECT(wifi->mainwindow), "view-press");
    gtk_window_set_focus(GTK_WINDOW(mainwindow), view);
			g_idle_add((GSourceFunc)cb_active_device, wifi);
    return 0;
}


